<template>
  <div class="app-container">
    <!-- 添加或修改出境申请对话框 -->
      <el-form ref="form" :model="form" :rules="rules" label-width="150px" style="margin-left: 50px;margin-top: 30px;width: 700px">
        <el-form-item label="申请人ID" prop="eno">
          <el-input v-model="form.eno" placeholder="请输入申请人ID" />
        </el-form-item>
        <el-form-item label="出国（境）目的地" prop="destination" label-width="150px">
          <el-input v-model="form.destination" placeholder="请输入出国（境）目的地" />
        </el-form-item>
        <el-form-item label="出国（境）目的、事由" prop="purpose" label-width="170px">
          <el-input v-model="form.purpose" placeholder="请输入出国（境）目的、事由" />
        </el-form-item>
        <el-form-item label="出国（境）时间长度（天数）" prop="duration" label-width="210px">
          <el-input v-model="form.duration" placeholder="请输入出国（境）时间长度（天数）" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm">申请</el-button>
          <el-button>取消</el-button>
        </el-form-item>
      </el-form>
  </div>
</template>

<script>
import { listExitApplications, getExitApplications, delExitApplications, addExitApplications, updateExitApplications } from "@/api/system/exitApplications";
import {addEvection} from "@/api/system/evection";

export default {
  name: "ExitApplications",
  dicts: ['stamp'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 出境申请表格数据
      exitApplicationsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        eno: null,
        destination: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        eno: [
          { required: true, message: "申请人ID不能为空", trigger: "blur" }
        ],
        destination: [
          { required: true, message: "出国（境）目的地", trigger: "blur" }
        ],
        purpose: [
          { required: true, message: "出国（境）目的、事由", trigger: "blur" }
        ],
        duration: [
          { required: true, message: "出国（境）时间长度（天数）", trigger: "blur" }
        ],
        createdAt: [
          { required: true, message: "$comment不能为空", trigger: "blur" }
        ],
        updatedAt: [
          { required: true, message: "$comment不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    onSubmit() {
      addEvection(this.form).then(response => {
        this.$modal.msgSuccess("新增成功");
        this.reset();
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        eno: null,
        destination: null,
        purpose: null,
        duration: null,
        status: null,
        createdAt: null,
        updatedAt: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加出境申请";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getExitApplications(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改出境申请";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateExitApplications(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addExitApplications(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除出境申请编号为"' + ids + '"的数据项？').then(function() {
        return delExitApplications(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/exitApplication/export', {
        ...this.queryParams
      }, `exitApplications_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
